<?php

class cacheAction extends backendAction
{
    public function _initialize() {
        parent::_initialize();
    }
    //home
    public function index() {
        $this->display();
    }
    //action show
    public function clear() {
        $type = $this->_get('type','trim');
        $obj_dir = new Dir;
        switch ($type) {
            case 'field':
                is_dir(DATA_PATH . '_fields/') && $obj_dir->del(DATA_PATH . '_fields/');
                break;
            case 'tpl':
                is_dir(CACHE_PATH) && $obj_dir->delDir(CACHE_PATH);
                break;
            case 'data':
                is_dir(DATA_PATH) && $obj_dir->del(DATA_PATH);
                is_dir(TEMP_PATH) && $obj_dir->delDir(TEMP_PATH);
                break;
            case 'runtime':
                @unlink(RUNTIME_FILE);
                break;
            case 'logs':
                is_dir(LOG_PATH) && $obj_dir->delDir(LOG_PATH);
                break;
            case 'js':
                is_dir(WEB_ROOT. '/rc.zhiyoumeng.com/wx/runjs/') && $obj_dir->del(WEB_ROOT. '/rc.zhiyoumeng.com/wx/runjs/');
                break;
            case 'temp':
                is_dir(ZYB_DATA_PATH .'upload/temp/') && $obj_dir->delDir(ZYB_DATA_PATH .'upload/temp/');
                break;
        }
        $this->ajaxReturn(1);
    }
    //cut action
    public function qclear() {
        $obj_dir = new Dir;
        is_dir(DATA_PATH . '_fields/') && $obj_dir->del(DATA_PATH . '_fields/');
        is_dir(CACHE_PATH) && $obj_dir->delDir(CACHE_PATH);
        is_dir(DATA_PATH) && $obj_dir->del(DATA_PATH);
        is_dir(TEMP_PATH) && $obj_dir->delDir(TEMP_PATH);
        is_dir(LOG_PATH) && $obj_dir->delDir(LOG_PATH);
        is_dir(WEB_ROOT. 'rc.zhiyoumeng.com/wx/runjs/') && $obj_dir->del(WEB_ROOT. 'rc.zhiyoumeng.com/wx/runjs/');
		is_dir(WEB_ROOT. 'wx.zhiyoumeng.com/data/runtime/') && $obj_dir->delDir(WEB_ROOT. 'wx.zhiyoumeng.com/data/runtime/');
        is_dir(WEB_ROOT. 'zyadmin.zhiyoumeng.com/runtime/') && $obj_dir->delDir(WEB_ROOT. 'zyadmin.zhiyoumeng.com/runtime/');
        @unlink(RUNTIME_FILE);
        $this->ajaxReturn(1,'清除成功!!');
    }
    //更换名片信息 
    public function jhcard(){
        	  $this->display();
	}
    //safe setting
    public function jsafe(){
    	$type = $this->_get('type','trim');
        if($type){
		        switch ($type) {
		            case 'index':
		                    //配置
							$root_dir = WEB_ROOT.'data/'; // 根目录
							$default_content = ' ';  //生成的index.html文件默认内容
							$dirs = rscandir($root_dir, 'dir');
							$current_path = str_replace('\\', '/', dirname(__FILE__));
							$source_path = $current_path.'/index.html';
							file_put_contents($source_path, $default_content);
							foreach ($dirs as $d)
							{
								copy($source_path, $d.'index.html');
							}
		                break;
		              case 'bom':
		                   //去除bom
							$basedir =WEB_ROOT;
							checkdir($basedir);
		              break;
		              
		              case 'admin':
		                   //更改后台文件入口
		                   $name=$this->_get('name','trim');
		                   $oldname= M('setting')->where(array('name'=>'adm_enter'))->getField('data');
		                   M('setting')->where(array('name'=>'adm_enter'))->save(array('data'=>$name));
		                   rename(WEB_ROOT.$oldname.'.php',WEB_ROOT.$name.'.php');
		                   //清除缓存
		                   $this->qclear();
		              break;
		              case 'putindex':
		                   RA('home/index/index','index.html');
		              break;
		        }
		        $this->ajaxReturn(1);
        }else{
        	  $this->display();
        }

    }
    //清除对应会员名片信息
	public  function clear_udata(){
		$uid=$this->_request('uid','strip_tags');
		$pwd=$this->_request('pwd','strip_tags');
		if(!$pwd||$pwd!='pwdzyuid'){
			$this->ajaxReturn(0,'管理密码不正确没权限');
		}
		if(!in_array(($uid), explode(',',C('zyb_updata_uidcard'))))
		{
			$this->ajaxReturn(0,'您的uid未设置权限');
		}

		if(!$uid){
			$this->ajaxReturn(0,'uid必须填写');
		}
		M('album_img')->where(array('uid'=>$uid))->delete();
		M('album')->where(array('uid'=>$uid))->delete();
		M('can')->where(array('uid'=>$uid))->delete();
		M('zuiji')->where(array('uid'=>$uid))->delete();
		M('zuiji_detail')->where(array('uid'=>$uid))->delete();
		M('zuiji_img')->where(array('uid'=>$uid))->delete();
		$this->ajaxReturn(1,'恭喜您操作成功！');
	}

	//为别人做名片，替换名片信息
	public  function updata_udata(){
		$uid=$this->_request('uid','strip_tags');
		$touid=$this->_request('touid','strip_tags');
		$pwd=$this->_request('pwd','strip_tags');
		if(!in_array(($uid), explode(',',C('zyb_updata_uidcard'))))
		{
			$this->ajaxReturn(0,'您的uid未设置权限');
		}

		if(!$pwd||$pwd!='pwdzyuid'){
			$this->ajaxReturn(0,'管理密码不正确没权限');
		}
		if(!$uid||!$touid){
			$this->ajaxReturn(0,'对方的uid和自己的uid都必须填写');
		}
		$data['uid']=$touid;
		M('album_img')->where(array('uid'=>$uid))->save($data);
		M('album')->where(array('uid'=>$uid))->save($data);
		M('can')->where(array('uid'=>$uid))->save($data);
		M('zuiji')->where(array('uid'=>$uid))->save($data);
		M('zuiji_detail')->where(array('uid'=>$uid))->save($data);
		M('zuiji_img')->where(array('uid'=>$uid))->save($data);

		$this->ajaxReturn(1,'恭喜您操作成功！');
	}
	//为别人修改名片信息头像
	public function save_head(){
		$uid=$this->_request('uid','strip_tags');
		$touid=$this->_request('touid','strip_tags');
		$pwd=$this->_request('pwd','strip_tags');
		if(!in_array(($uid), explode(',',C('zyb_updata_uidcard'))))
		{
			$this->ajaxReturn(0,'您的uid未设置权限');
		}

		if(!$pwd||$pwd!='pwdzyuid'){
			$this->ajaxReturn(0,'管理密码不正确没权限');
		}
		if(!$uid||!$touid){
			$this->ajaxReturn(0,'对方的uid和自己的uid都必须填写');
		}
		$list =M('card')->where(array('uid'=>$touid))->getField('id');
		if(!$list){
           $this->ajaxReturn(0,'没有被修改会员的名片');
		}
		//根据自己的uid去查询自己的头像
		$user= M('card')->where(array('uid'=>$uid))->find();
		$data['img']=$user['img'];
		$data['ategory']=$user['ategory'];
		$data['wxategory']=$user['wxategory'];
		$row = M('card')->where(array('uid'=>$touid))->save($data);
		if($row!==false){
		  $this->ajaxReturn(1,'恭喜您操作成功！');
		}else{
		  $this->ajaxReturn(0,'恭喜您操作失败！');
		}
	}
   //修改名片滚动图
   public function banner(){
       $uid=$this->_request('uid','strip_tags');
		$touid=$this->_request('touid','strip_tags');
		$pwd=$this->_request('pwd','strip_tags');
		if(!in_array(($uid), explode(',',C('zyb_updata_uidcard'))))
		{
			$this->ajaxReturn(0,'您的uid未设置权限');
		}

		if(!$pwd||$pwd!='pwdzyuid'){
			$this->ajaxReturn(0,'管理密码不正确没权限');
		}
		if(!$uid||!$touid){
			$this->ajaxReturn(0,'对方的uid和自己的uid都必须填写');
		}
		$list =M('card')->where(array('uid'=>$touid))->getField('id');
		if(!$list){
           $this->ajaxReturn(0,'没有被修改会员的名片');
		}
		//先删除修改用户的滚动图片
		M('card_banner')->where(array('uid'=>$touid))->delete();
		$data['uid']=$touid;
		$user=M('card_banner')->where(array('uid'=>$uid))->save($data);
		if($user!==false){
		  $this->ajaxReturn(1,'恭喜您操作成功！');
		}else{
		  $this->ajaxReturn(0,'操作失败！');
		}
   }
   //为别人修改名片背景图片
   public function bgimg(){
	    $uid=$this->_request('uid','strip_tags');//自己的uid
		$touid=$this->_request('touid','strip_tags');//对方uid
		$pwd=$this->_request('pwd','strip_tags');
		if(!in_array(($uid), explode(',',C('zyb_updata_uidcard'))))
		{
			$this->ajaxReturn(0,'您的uid未设置权限');
		}
		if(!$pwd||$pwd!='pwdzyuid'){
			$this->ajaxReturn(0,'管理密码不正确没权限');
		}
		if(!$uid||!$touid){
			$this->ajaxReturn(0,'对方的uid和自己的uid都必须填写');
		}
		$list =M('card')->where(array('uid'=>$touid))->getField('id');
		if(!$list){
           $this->ajaxReturn(0,'没有被修改会员的名片');
		}
		
		//查找自己的背景图
		$bgimg = M('card')->where(array('uid'=>$uid))->getField('bgimg');

		$data['bgimg']=$bgimg;
		$user=M('card')->where(array('uid'=>$touid))->save($data);
		if($user!==false){
		  $this->ajaxReturn(1,'恭喜您操作成功！');
		}else{
		  $this->ajaxReturn(0,'操作失败！');
		}
   }

}

//重写入
function rewrite ($filename, $data) {
		$filenum = fopen($filename, "w");
		flock($filenum, LOCK_EX);
		fwrite($filenum, $data);
		fclose($filenum);
}
//检查Bom
function checkBOM ($filename) {
			$contents = file_get_contents($filename);
			$charset[1] = substr($contents, 0, 1);
			$charset[2] = substr($contents, 1, 1);
			$charset[3] = substr($contents, 2, 1);
			if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {
				 $rest = substr($contents, 3);
				 rewrite ($filename, $rest);
				 return ("<font color=red>BOM found, automatically removed.");
			}
		  return ("BOM Not Found.");
}

//检查目录
function checkdir($basedir){
		if ($dh = opendir($basedir)) {
				 while (($file = readdir($dh)) !== false) {
					 if ($file != '.' && $file != '..'){
						 if (!is_dir($basedir."/".$file)) {
							 checkBOM($basedir/$file);
						 }else{
						     $dirname = $basedir."/".$file;
						     checkdir($dirname);
						  }
					  }
				 }
		   closedir($dh);
		}
}

//生成html
function rscandir($base = '',$return = 'all', &$data = array()  ) {
		$ds = DIRECTORY_SEPARATOR;
		$base = rtrim($base,$ds).$ds;
		$array = array_diff(scandir($base), array('.', '..', '.svn'));
		foreach ($array as $value) {
				if (is_dir($base . $value)) {
						if($return != 'file')$data[] = $base . $value . $ds;
						 $data = rscandir($base . $value . $ds, $return, $data );
				} elseif (is_file($base . $value)) {
						if($return == 'dir') continue;
						  $data[] = $base . $value;
						}
		}
		return $data;
}